package com.serkol.ams.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.serkol.ams.entity.UsersInvitationRelationship;
import com.serkol.ams.mapper.UsersInvitationRelationshipMapper;
import com.serkol.ams.service.UsersInvitationRelationshipService;
import org.springframework.stereotype.Service;

/**
 * @author pingan
 * @description 针对表【users_invitation_relationship】的数据库操作Service实现
 * @createDate 2022-09-23 12:31:50
 */
@Service
public class UsersInvitationRelationshipServiceImpl extends ServiceImpl<UsersInvitationRelationshipMapper, UsersInvitationRelationship>
        implements UsersInvitationRelationshipService {

    @Override
    public boolean invalidUsers(Long parentUserId, Long userId) {
        QueryWrapper<UsersInvitationRelationship> query = new QueryWrapper<>();

        query.lambda().eq(UsersInvitationRelationship::getUserId, parentUserId).eq(UsersInvitationRelationship::getRelationUserId, userId);
        query.lambda().or(o -> o.eq(UsersInvitationRelationship::getRelationUserId, parentUserId).eq(UsersInvitationRelationship::getUserId, userId));

        boolean exists = this.baseMapper.exists(query);
        if (!exists) {
            UsersInvitationRelationship usersInvitationRelationship = new UsersInvitationRelationship();
            usersInvitationRelationship.setRelationUserId(userId);
            usersInvitationRelationship.setUserId(parentUserId);
            return this.save(usersInvitationRelationship);
        } else {
            return false;
        }

    }
}




